/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.regions;

import ca.trader.utils.TraderTreeSet;
import java.io.Serializable;
import java.util.Iterator;
import javax.persistence.Embeddable;
import javax.persistence.Transient;

/**
 * Class to represent a country.  Contains the name of the country and the regions in the country.  The regions in that country
 * are only available if set during constructing the country object.
 * You can iterate over an instance of this class, which will return all regions in this country.
 * @author Ahsan
 */
@Embeddable
public class Country implements Iterable<Region>, Comparable<Country>, Serializable
{

	 /* The name of the country */	 
	 private String countryName;
	 /* The regions that are in the country */
	 @Transient
	 private TraderTreeSet<Region> regions = new TraderTreeSet<>();

	 /**
	  * Initiate a country with a null name and no regions
	  */
	 public Country()
	 {
	 }
	 
	 /**
	  * initiate a country with a name and no regions
	  * @param name the name of the country
	  */
	 public Country(String name)
	 {
		  this.countryName = name;
	 }

	 /**
	  * set the name of a the country
	  * @param name the name of the country
	  */
	 public void setCountryName(String name)
	 {
		  this.countryName = name;
	 }


	 /**
	  * Get the name of the country
	  * @return the country name.  Returns null if the name has not been set
	  */
	 public String getCountryName()
	 {
		  return this.countryName;
	 }


	 /**
	  * Add a region to the country
	  * @param region the region to add
	  */
	 public void addRegion(Region region)
	 {
		  regions.add(region);
	 }

	 /**
	  * Adds a list of the regions to the country
	  * @param regions the list of regions
	  */
	 public void addRegions(Region[] regions)
	 {
		  for (Region region : regions)
		  {
			   addRegion(region);
		  }
	 }

	 /**
	  * Returns all the regions in this country.
	  * @return all the regions in the country.
	  */
	 public Region[] getRegionList()
	 {
		  return regions.toArray(new Region[0]);
	 }

	 /**
	  * returns true if the country contains the given region
	  * @param region the region to search
	  * @return true if the Region exists.  False if not.
	  */
	 public boolean containsRegion(Region region)
	 {
		  return regions.contains(region);
	 }
	 
	 public boolean containsRegion (String name)
	 {
		  return containsRegion (new Region (name));
	 }

	 /**
	  * Finds a region with the name <i>name</i>
	  * @param name the name of the region to search
	  * @return the region.  Returns null if no region with the name exists.
	  */
	 public Region getRegion(String name)
	 {
		  return regions.findIdenticalItem(new Region(name));
	 }
	 
	 /**
	  * Compares this Country object to another object.
	  * @param o the other object
	  * @return true if the other object is an instance of Country, and has the same name as this object.  False otherwise.
	  */
	 @Override
	 public boolean equals(Object o)
	 {
		  if (!(o instanceof Country))
		  {
			   return false;
		  }

		  return this.countryName.equals(((Country) o).getCountryName());
	 }

	 /**
	  *
	  * @return  an iterator over all the regions in the country
	  */
	 @Override
	 public Iterator<Region> iterator()
	 {
		  return regions.iterator();
	 }

	 /**
	  * Compares the name of this country to the name of another country
	  * @param o The other country
	  * @return string comparison of the name of the country.
	  */
	 @Override
	 public int compareTo(Country o)
	 {
		  return this.getCountryName().compareTo(o.getCountryName());
	 }

	 /**
	  * 
	  * @return the name of the country
	  */
	 @Override
	 public String toString()
	 {
		  return this.getCountryName();
	 }

}
